package com.impetus.project.skilltest.server.util;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.InitialContext;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;



public class HibernateUtil {

    private static Configuration configuration;
    private static SessionFactory sessionFactory;
    
    private static Logger logger = Logger.getLogger(HibernateUtil.class.getName());
    
    static
    {
    	try{
    		configuration = new Configuration();
    		
    		InitialContext initialContext = new javax.naming.InitialContext();  
    		String configDir = (String)initialContext.lookup("java:comp/env/CONFIG_DIR");  
    		
        	System.out.println("configDir: "+configDir);
        	
        	//File file = new File(configFile);
        	File file = new File(configDir + "/hibernate.cfg.xml");
        	
        	configuration.configure(file);
        	
        	rebuildSessionFactory(configuration);
        	
    	}catch(Throwable th)
    	{
    		th.printStackTrace();
    		logger.log(Level.SEVERE, th.getMessage(), th);
    		System.exit(-1);
    	}
    	
    	
    }
    /**
     * Rebuild the SessionFactory with the given Hibernate Configuration.
     * <p>
     * HibernateUtil does not configure() the given Configuration object,
     * it directly calls buildSessionFactory(). This method also closes
     * the old static variable SessionFactory before, if it is still open.
     *
     * @param cfg
     */
     private static void rebuildSessionFactory(Configuration cfg) {
        //log.debug("Rebuilding the SessionFactory from given Configuration");
        if (sessionFactory != null && !sessionFactory.isClosed())
        {	
            sessionFactory.close();
            cfg.buildSessionFactory();
        } else {
            
            sessionFactory = cfg.buildSessionFactory();
        }
        configuration = cfg;
     }
     
     private static void rebuildSessionFactory() {
    	 rebuildSessionFactory(configuration);
     }

 	/**
 	 * The behaviour of this method depends on the session context you have
 	 * configured. This factory is intended to be used with a hibernate.cfg.xml
 	 * including the following property <property
 	 * name="current_session_context_class">thread</property> This would return
 	 * the current open session or if this does not exist, will create a new
 	 * session
 	 *
 	 * @return Session
 	 */
 	public Session getCurrentSession() {
 		return sessionFactory.getCurrentSession();
 		//TODO: what if multiple session obtained for same class
 	}
 	
     /**
      * Closes the current SessionFactory and releases all resources.
      * <p>
      * The only other method that can be called on HibernateUtil
      * after this one is rebuildSessionFactory(Configuration).
      */
     public static void shutdown() {
         //log.debug("Shutting down Hibernate");
         // Close caches and connection pools
         getSessionFactory().close();

         // Clear static variables
         sessionFactory = null;
     }
     
     /**
      * Returns the global SessionFactory either from a static variable or a JNDI lookup.
      *
      * @return SessionFactory
      */
     public static SessionFactory getSessionFactory() {
         
         if (sessionFactory == null) {
             rebuildSessionFactory();
         }
         return sessionFactory;
     }



}

